package xin.yangshuai.basic01.proxy;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * SimpleProxy
 *
 * @author shuai
 * @date 2020/7/27
 */
public class SimpleProxy {
    public static void set(String host, Integer port) {
        String proxyHost = host;
        String proxyPort = Integer.toString(port);

        System.setProperty("http.proxyHost", proxyHost);
        System.setProperty("http.proxyPort", proxyPort);

        // 对https也开启代理
        System.setProperty("https.proxyHost", proxyHost);
        System.setProperty("https.proxyPort", proxyPort);
    }


    public static void main(String[] args) throws IOException {
        SimpleProxy.set("192.168.0.10", 3128);
        URL url = new URL("https://www.baidu.com");
        URLConnection connection = url.openConnection();
        connection.connect();
        InputStream inputStream = connection.getInputStream();
        byte[] bytes = new byte[1024];
        while (inputStream.read(bytes) >= 0) {
            System.out.println(new String(bytes));
        }
    }

}
